// Include Files
#include "Types\File.h"

// file_t Implementation

bool_t file_t::Open( char_t const *_szFilename, char_t const *_szMode )
{
	_wfopen_s(&m_pFile, _szFilename, _szMode);
	return m_pFile != CE_NULL;
}

void file_t::Close()
{
	clearerr(m_pFile);
	fclose(m_pFile);
}

void file_t::ReadLine( char_t *_szBuffer, uint_t _uLength )
{
	uint_t uCurrent;
	for( uCurrent = 0; uCurrent < _uLength; ++uCurrent )
	{
		int_t wc = fgetc(m_pFile);
		if( wc == EOF || wc == '\n' ) break;

		_szBuffer[uCurrent] = (char_t)wc;
	}
	_szBuffer[uCurrent] = '\0';
}

uint_t file_t::GetSize()
{
	// count the number of characters in the file
	uint_t uCurr = ftell(m_pFile);
	fseek(m_pFile, 0, SEEK_END);
	uint_t uLength = ftell(m_pFile);
	fseek(m_pFile, uCurr, SEEK_SET);
	return uLength;
}

uint_t file_t::Read( byte_t *_pBuffer, uint_t _uSize )
{
	return fread_s(_pBuffer, _uSize, _uSize, 1, m_pFile);
}

uint32_t file_t::ReadUInt32()
{
	uint32_t ret;
	fread_s(&ret, 4, 4, 1, m_pFile);
	return ret;
}
